package cn.gok.manage.web;

import cn.gok.manage.service.IShippingService;
import cn.gok.pojo.Shipping;
import cn.gok.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * @author: 鸭鸭
 * @ClassName:ShippingController
 * @Package:cn.gok.manage.web
 * @Description:
 * @date: 2021/8/4
 * @time: 16:45
 */
@Controller
@RequestMapping("/shipping")
public class ShippingController {
    @Autowired
    IShippingService shippingService;
    @RequestMapping(value = "/add.do",method = RequestMethod.GET)
    @ResponseBody
    public ResultData addShipping(@RequestParam("userId") int userId,
                                  @RequestParam("receiverName") String receiverName,
                                  @RequestParam("receiverPhone") String receiverPhone,
                                  @RequestParam("receiverMobile") String receiverMobile,
                                  @RequestParam("receiverProvince") String receiverProvince,
                                  @RequestParam("receiverCity") String receiverCity,
                                  @RequestParam("receiverAddress") String receiverAddress,
                                  @RequestParam("receiverZip") String receiverZip){
        Shipping record = new Shipping();
        record.setUserId(userId);
        record.setReceiverName(receiverName);
        record.setReceiverPhone(receiverPhone);
        record.setReceiverMobile(receiverMobile);
        record.setReceiverProvince(receiverProvince);
        record.setReceiverCity(receiverCity);
        record.setReceiverAddress(receiverAddress);
        record.setReceiverZip(receiverZip);
        record.setCreateTime(new Date());
        record.setUpdateTime(new Date());

        return shippingService.insertShipping(record);
    }

    /*
    * 通过地址id删除地址
    * */
    @RequestMapping(value = "/del.do",method = RequestMethod.GET)
    @ResponseBody
    public ResultData delShipping(@RequestParam("shippingId") int id){
        return shippingService.deleteShippingById(id);
    }

    /*
    * 登录状态更新地址
    * */
    @RequestMapping(value = "/update.do",method = RequestMethod.GET)
    @ResponseBody
    public ResultData updateShippingById(@RequestParam("shippingId") int id,
                                         @RequestParam("receiverName") String receiverName,
                                         @RequestParam("receiverPhone") String receiverPhone,
                                         @RequestParam("receiverMobile") String receiverMobile,
                                         @RequestParam("receiverProvince") String receiverProvince,
                                         @RequestParam("receiverCity") String receiverCity,
                                         @RequestParam("receiverAddress") String receiverAddress,
                                         @RequestParam("receiverZip") String receiverZip){
        Shipping record = new Shipping();
        record.setId(id);
        record.setReceiverName(receiverName);
        record.setReceiverPhone(receiverPhone);
        record.setReceiverMobile(receiverMobile);
        record.setReceiverProvince(receiverProvince);
        record.setReceiverCity(receiverCity);
        record.setReceiverAddress(receiverAddress);
        record.setReceiverZip(receiverZip);
        record.setUpdateTime(new Date());
        return shippingService.updateShippingInLogin(record);
    }

    /*
    * 选中查看具体的地址
    * */
    @RequestMapping(value = "/select.do",method = RequestMethod.GET)
    @ResponseBody
    public ResultData SelectShippingById(@RequestParam("shippingId") int id){
        return shippingService.selectShippingById(id);
    }

    /*
    * 查询地址列表
    * */
    @RequestMapping(value = "/list.do",method = RequestMethod.GET)
    @ResponseBody
    public ResultData  SelectAllShipping(@RequestParam("pageNum") int pageNum,
                                         @RequestParam("pageSize") int pageSize){
        return shippingService.selectAllShipping(pageNum,pageSize);
    }


}
